<template>
  <div v-loading="true" style="height: 100%" element-loading-text="登录中,请稍等..."></div>
</template>

<script>
  import { mapActions } from 'vuex'
  import { setStore } from '@/util/store'
  import { hasAuthority } from '@/util/auth'

  export default {
    name: 'SsoLogin',
    props: {
      code: String,
      title: String,
      type: String
    },
    async created() {
      document.title = this.title
      setStore('appName', this.title)
      setStore('homeType', this.type)
      try {
        await this.handleLogin({ grantType: 'mobile', code: this.code, auto: true })
        if (this.type === 'video') {
          if (hasAuthority('VideoManager')) {
            await this.$router.replace('/video/VideoManager')
          } else {
            await this.$router.replace('/videoList')
          }
        } else {
          await this.$router.replace(this.$route.query.redirect || '/')
        }
      } catch (e) {
        this.$message.error(e.message || '登录失败')
      }
    },
    methods: {
      ...mapActions('user', ['handleLogin'])
    }
  }
</script>
